
// Definition for a Node.
function Node(val,children) {
   this.val = val;
   this.children = children;
};


function createTree(arr) {
  if (arr.length < 1)
    return null;
  let NodeArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] == null) {
      NodeArr.push(null);
      continue;
    }
    let tempNode = new TreeNode(arr[i], null, null);
    NodeArr.push(tempNode);
  }
  for (let i = 0; i < arr.length; i++) {
    if (NodeArr[i] == null) continue;
    if (2 * i + 1 < arr.length)
      NodeArr[i].left = NodeArr[2 * i + 1];
    if (2 * i + 2 < arr.length)
      NodeArr[i].right = NodeArr[2 * i + 2];
  }
  return NodeArr[0];
}

function preShow(node) {
  if (node == null)
    return;
  console.log(node.val);
  preShow(node.left);
  preShow(node.right);
}
/**
 * @param {Node|null} root
 * @return {number[][]}
 */
var levelOrder = function(root) {
  if(root == null)
    return [];
  if(root.children.length == 0)
    return [[root.val]];
  let result = [], queue = [{
    node: root,
    depth: 0
  }];
  while(queue.length) {
    let tempNode = queue[0];
    queue.splice(0, 1);
    for(let i = 0; i < tempNode.node.children.length; i++) {
      if(!tempNode.node.children[i]) continue;
      queue.push({
        node: tempNode.node.children[i],
        depth: tempNode.depth + 1
      });
    }
    if(result[tempNode.depth]) {
      result[tempNode.depth].push(tempNode.node.val);
    } else {
      result[tempNode.depth] = [tempNode.node.val];
    }
  }
  return result;
};
let root = [1,null,3,2,4,null,5,6];
console.log(levelOrder(root));
root = [1,null,3,2,4,null,5,6];
console.log(levelOrder(root));
